package com.liuweiheng.cloudproviderpayment8002.common.controller;

import cn.hutool.core.util.StrUtil;
import com.liuweiheng.base.entity.R;
import com.liuweiheng.cloudproviderpayment8002.common.service.UserService;
import com.liuweiheng.cloudproviderpayment8002.util.RedisUtil;
import com.liuweiheng.cloudproviderpayment8002.common.service.BaseService;
import com.liuweiheng.cloudproviderpayment8002.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * 测试控制器
 *
 * @author lwh
 */
@RestController
@RequestMapping(value="/payment/base")
@Api(tags="测试Controller")
@Slf4j
public class BaseController {

    @Value("${server.port}")
    private String serverPort;

    /**
     * 基础服务接口
     */
    @Autowired
    private BaseService baseService;

    @Autowired
    private UserService userService;



    @GetMapping("/index")
    public R index() {
        return R.ok("hello");
    }

    @ApiOperation("/登录")
    @PostMapping("/login")
    public R login(@RequestBody User user) throws IOException {

        R loginResult = baseService.login(user);

        if (loginResult.get(R.CODE).equals(R.SUCCESS_CODE)) {
            return R.ok("auth success").putData(userService.selectUserByUsername(user.getUsername()));
        } else {
//            response.sendRedirect("/base/error");
            return R.error("auth fail");
        }

//        return null;
    }

    @ApiOperation("退出登录")
    @GetMapping("/logout")
    public R logout() {
        SecurityUtils.getSubject().logout();
        return R.ok("成功退出登录");
    }


    @GetMapping("/admin")
    @RequiresRoles(value={"admin"})
    public R admin() {
        return R.ok("管理员你好");
    }


    @GetMapping("/error")
    public R error() {
        String age = String.valueOf(RedisUtil.getValue("age"));
        System.out.println(age);
        return R.error("auth error");
    }


    @GetMapping("/zk")
    public R paymentZk() {
        return R.ok("springcloud for zookeeper：" + serverPort + StrUtil.uuid());
    }



}
